//
//  DabiaoTableViewCell.m
//  GetPowerDBO
//
//  Created by 周博 on 2017/7/6.
//  Copyright © 2017年 BogoZhou. All rights reserved.
//

#import "DabiaoTableViewCell.h"
#import "CurveViewController.h"
#import "HistoryDabiaoViewController.h"

@interface DabiaoTableViewCell ()
{

}
@property (nonatomic,strong) NSString *shopId;
@property (nonatomic,strong) DabiaoModel *dabiaoModel;
@end

@implementation DabiaoTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _viewContent.layer.masksToBounds = YES;
    _viewContent.layer.cornerRadius = 10;
    
    _viewContent.layer.borderColor = [kColorFrom0x(0xf5f5f5) CGColor];
    _viewContent.layer.borderWidth = 2.f;
    
    UITapGestureRecognizer *tapRecognizerTel=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(callFun)];
    
    _labelPhoneNum.userInteractionEnabled=YES;
    [_labelPhoneNum addGestureRecognizer:tapRecognizerTel];
    
    _viewImage.layer.masksToBounds = YES;
    _viewImage.layer.cornerRadius = 10;
    
    _viewGo.layer.masksToBounds = YES;
    _viewGo.layer.cornerRadius = 10;
}

- (void)showDataWithModel:(DabiaoModel *)model{
    _shopId = model.id;
    
    _dabiaoModel = model;
    
    [_imageViewHeader sd_setImageWithURL:[NSURL URLWithString:model.img_url] placeholderImage:[UIImage imageNamed:@""]];
    
    _labelShopName.text = model.name;
    
    _labelMerPerson.text = [NSString stringWithFormat:@"联系人:%@",model.contact];
    
    _labelPhoneNum.text = model.tel;
    
    _labelAnzhuangshebei.text = [NSString stringWithFormat:@"安装设备:%ld",model.already_charging_num.integerValue];
    
    NSLog(@"anzhuang -> %@",model.already_charging_num);
    
    _labelZaixianshebei.text = [NSString stringWithFormat:@"使用设备:%ld",model.use_charging_num.integerValue];
    
    _labelZaiXianLu.text = [NSString stringWithFormat:@"使用率:%.0lf%%",model.already_charging_num.integerValue == 0 ? model.already_charging_num.doubleValue : (model.use_charging_num.doubleValue / model.already_charging_num.doubleValue *100)];
}

- (IBAction)CurveJumpButtonClick:(UIButton *)sender {
    UIStoryboard *storyboard = kMainStroyboard;
    
    HistoryDabiaoViewController *historyDabiao = [storyboard instantiateViewControllerWithIdentifier:@"HistoryDabiaoViewController"];
    historyDabiao.shopId = _shopId;
    historyDabiao.beginDate = [BGDateHelper getTimeArrayByTimeString:[BGDateHelper getTimeStempByString:_dabiaoModel.created havehh:YES]][0];
    [[self viewController].navigationController pushViewController:historyDabiao animated:YES];
    
//    kAlert(@"功能正在完善，很快就好别急");
}


- (UIViewController *)viewController {
    UIResponder *next = self.nextResponder;
    do {
        //判断响应者是否为视图控制器
        if ([next isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)next;
        }
        next = next.nextResponder;
    } while (next != nil);
    
    return nil;
}

- (void)callFun{
    
    NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",_labelPhoneNum.text];
    UIWebView *callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [[self viewController].view addSubview:callWebview];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
